/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bionic.commands.user;

import com.bionic.commands.common.ICommand;
import com.bionic.dao.UserDAO;
import com.bionic.entity.Users;
import com.bionic.manager.Config;
import com.bionic.manager.Message;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Obuhov D.G.
 */
public class CommandLogin implements ICommand {

    private static final String LOGIN = "login";
    private static final String PASSWORD = "password";

    @Override
    public String execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession(true);
        
       /* Enumeration e = session.getAttributeNames();
        while (e.hasMoreElements()) {
        String name = (String) e.nextElement();
        System.out.println("L+ session "+name + ": " + session.getAttribute(name));
        }
        
        e = request.getAttributeNames();
        while (e.hasMoreElements()) {
        String name = (String) e.nextElement();
        System.out.println("L+ request "+name + ": " + session.getAttribute(name));
        }*/
        
        String page =  Config.getInstance().getProperty(Config.LOGIN);

        UserDAO userDAO = new UserDAO();

        String login = request.getParameter(LOGIN);
        String password = request.getParameter(PASSWORD);
        
        Users user = userDAO.findByUserAndPassword( login, password);
        if (user != null) {
            session.setAttribute("currentUser", user);
            request.setAttribute("user", login);
            
            page = Config.getInstance().getProperty(Config.MAIN);
        } else {
            request.setAttribute("error","login="+login+"| "+"pass="+password +"| "+ Message.getInstance().getProperty(Message.LOGIN_ERROR));
            page = Config.getInstance().getProperty(Config.ERROR);
        }
        
        return page;

        //проверяем логику
    }
}
